//mazeBoard.h的实现文件

#include "mazeBoard.h"

mazeBoard::mazeBoard(QWidget *parent) : QWidget(parent)
{
    //qgl = new QGridLayout;

    int tmpMaze[15][15];

    //初始化迷宫对象
    for(int t1 = 0; t1 < 15; t1 ++)
    {
        for(int t2 = 0; t2 < 15; t2 ++)
        {
            //qgl->addWidget(&mb[t1][t2], t1+1, t2+1);
            tmpMaze[t1][t2] = 0;
        }
    }

    //设置方格所属迷宫
    for(int t1 = 0; t1 < 50; t1 ++)
    {
        for(int t2 = 0; t2 < 50; t2 ++)
        {
            mb[t1][t2].setBoard(this);
            //qgl->addWidget(&mb[t1][t2], t1+1, t2+1);
        }
    }

    myMaze.setMap((int **)tmpMaze, 15);
    freshBoard();   //更新GUI迷宫

    //qgl->setSpacing(1);
    //this->setLayout(qgl);
    //this->setFixedSize(this->sizeHint());
    //this->setFixedSize(35*15, 35*15);

    boardModal = 1;     //设置为展示模式
    buildSize = 0;
}


maze* mazeBoard::getMaze()
{
    return &myMaze;
}


int mazeBoard::setBuildModal(int s)
{
    boardModal = 0;
    buildSize = s;
    //根据s更新GUI迷宫
    if(qgl != NULL)
    {
        delete qgl;
    }
    qgl = new QGridLayout;

    for(int t1 = 0; t1 < s; t1 ++)
    {
        for(int t2 = 0; t2 < s; t2 ++)
        {
            mb[t1][t2].setBuildState(0);
            qgl->addWidget(&mb[t1][t2], t1+1, t2+1);
        }
    }
    qgl->setSpacing(1);
    this->setLayout(qgl);
    this->update();
    //this->setFixedSize(this->sizeHint());
    this->setFixedSize(s*35, s*35);

    return 1;
}

int mazeBoard::saveBuildResult()
{
    int* tmpMaze = new int[buildSize * buildSize];
    //将建筑模式的迷宫地图保存到GUI迷宫中
    for(int t1 = 0; t1 < buildSize; t1 ++)
    {
        for(int t2 = 0; t2 < buildSize; t2 ++)
        {
            *(tmpMaze + (t1*buildSize+t2)) = mb[t1][t2].getBuildState();
        }
    }

    myMaze.setMap((int **)tmpMaze, buildSize);
    return 1;
}


int mazeBoard::setShowModal()
{
    boardModal = 1;
    freshBoard();
    /*
    updateBoard();
    if(qgl != NULL)
    {
        delete qgl;
    }
    qgl = new QGridLayout;

    int mazeSize = myMaze.getMapSize();
    for(int t1 = 0; t1 < mazeSize; t1 ++)
    {
        for(int t2 = 0; t2 < mazeSize; t2 ++)
        {
            qgl->addWidget(&mb[t1][t2], t1+1, t2+1);
        }
    }

    qgl->setSpacing(1);
    this->setLayout(qgl);
    //this->setFixedSize(this->sizeHint());
    this->setFixedSize(mazeSize*35, mazeSize*35);
    */
    return 1;
}


int mazeBoard::updateBoard()
{
    const int* tmpMaze = myMaze.getMazeMap();
    int mazeSize = myMaze.getMapSize();
    for(int t1 = 0; t1 < mazeSize; t1 ++)
    {
        for(int t2 = 0; t2 < mazeSize; t2 ++)
        {
            mb[t1][t2].setShowState(*(tmpMaze + t1 * 50 + t2));
        }
    }
    return 1;
}


int mazeBoard::freshBoard()
{
    updateBoard();
    int tmpMazeSize = myMaze.getMapSize();
    //根据迷宫对象重建GUI迷宫
    if(qgl != NULL)
    {
        delete qgl;
    }
    qgl = new QGridLayout;

    for(int t1 = 0; t1 < tmpMazeSize; t1 ++)
    {
        for(int t2 = 0; t2 < tmpMazeSize; t2 ++)
        {
            qgl->addWidget(&mb[t1][t2], t1+1, t2+1);
        }
    }

    qgl->setSpacing(1);
    this->setLayout(qgl);
    //this->setFixedSize(this->sizeHint());
    this->setFixedSize(35*tmpMazeSize, 35*tmpMazeSize);

    return 1;
}

int mazeBoard::showPath(int pn)
{
    int mazeSize = myMaze.getMapSize();
    //初始化路径
    for(int t1 = 0; t1 < mazeSize; t1 ++)
    {
        for(int t2 = 0; t2 < mazeSize; t2 ++)
        {
            mb[t1][t2].setPathState(0);
        }
    }

    if(pn == 0)
    {
        return 1;
    }
    //获取第pn条路径
    mazePath* tmpPath = myMaze.getFinalPath(pn);
    //更新GUI迷宫中的路径
    tmpPath = tmpPath->nextStep;
    while(tmpPath != NULL)
    {
        mb[tmpPath->xPos - 1][tmpPath->yPos - 1].setPathState(1);
        //mb[tmpPath->xPos - 1][tmpPath->yPos - 1].update();
        tmpPath = tmpPath->nextStep;
    }

    return 1;

}

int mazeBoard::getBoardModal()
{
    return boardModal;
}
